package com.extension.company.model.home;

import com.extension.company.Constant;
import com.extension.company.admin.R;
import com.extension.company.common.http.RetrofitHelper;
import com.extension.company.contract.home.OnlinePaySuccessContract;
import com.extension.company.model.api.RetrofitUrlApi;
import com.extension.company.model.entity.PaySuccessBean;
import com.extension.company.model.entity.RetailGoosListBean;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * 作者:zwk
 * 联系方式：497981239@qq.com
 * 时间：on 2019/8/7 08:36
 * 功能：在线支付成功后的model层
 */
public class OnlinePaySuccessModel implements OnlinePaySuccessContract.Model {
	RetrofitUrlApi retrofitUrlApi = RetrofitHelper.getRetrofit().create(RetrofitUrlApi.class);

	@Override
	public void commitOrder(String orderId, final OnlinePaySuccessContract.OrderCommitM2P callback) { // 确认订单
		retrofitUrlApi
				.commitOrderStatus(Constant.SIGN, Constant.TOKEN, orderId)
				.observeOn(AndroidSchedulers.mainThread())
				.subscribeOn(Schedulers.io())
				.subscribe(new Observer<PaySuccessBean>() {
					@Override
					public void onSubscribe(Disposable d) {

					}

					@Override
					public void onNext(PaySuccessBean paySuccessBean) {
						if (paySuccessBean.getCode() == 200) {
							String realMoney = paySuccessBean.getData().getTotal_fee();
							double realMoneyd = Double.parseDouble(realMoney) / 100;
							callback.m2p(realMoneyd);
						}

					}

					@Override
					public void onError(Throwable e) {

					}

					@Override
					public void onComplete() {

					}
				});
	}

	@Override
	public void retailGoodsList(final OnlinePaySuccessContract.retailListM2P callback) {  // 获取分销商品的列表
		retrofitUrlApi.retailList(Constant.SIGN, Constant.TOKEN)
				.observeOn(AndroidSchedulers.mainThread())
				.subscribeOn(Schedulers.io())
				.subscribe(new Observer<RetailGoosListBean>() {
					@Override
					public void onSubscribe(Disposable d) {

					}

					@Override
					public void onNext(RetailGoosListBean retailGoosListBean) {
						if (retailGoosListBean.getCode() == 200) {
							if (null != retailGoosListBean.getData()) {
								callback.m2p(retailGoosListBean.getData());
							}
						}

					}

					@Override
					public void onError(Throwable e) {

					}

					@Override
					public void onComplete() {

					}
				});

	}
}
